<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/20
 * Time: 2:01
 */

namespace app\common\model;


use think\Model;

class Shipping extends Model
{
    public function operation($data){
        $action = false;

        if(isset($data['id'])){
            $action = true;
        }else{
            $res = $this -> where('shipping_name', $data['shipping_name']) -> whereOr('shipping_code' ,$data['shipping_code']) -> value('id');
            if($res){
                return echoArr(0, '该快递已存在');
            }
        }

        $result = $this -> validate(true) -> allowField(true) -> isUpdate($action) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '操作成功');
        }
    }

    public function del($data){
        $result = $this -> destroy($data);
        if(false === $result){
            return echoArr(0, '操作失败');
        }else{
            return echoArr(1, '操作成功');
        }
    }

    /**
     * 更改状态
     */
    public function editStatus($data){
        $result = $this -> validate('Shipping.is_open') -> allowField(true) -> isUpdate(true) -> save($data);

        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '操作成功');
        }
    }
}